#include "mainwindow.h"
#include "FileManager.h"
#include <QApplication>
#include <QDebug>
#include <QHBoxLayout>
#include <QScreen>
#include <QSplitter>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    // 设置窗口标志，确保有最小化、最大化和关闭按钮
    setWindowFlags(Qt::Window | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);

    // 允许调整窗口大小
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 设置默认窗口大小
    resize(1000, 600);

    setupUI();
    connectSignals();

    FileManager::getInstance().loadFromJson("file_info.json");
    qDebug() << "文件信息缓存已加载";

    m_currentImageList = FileManager::getInstance().getFiles();
    updateImageList(m_currentImageList);
}

MainWindow::~MainWindow() {}

void MainWindow::setupUI()
{
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    QHBoxLayout *mainLayout = new QHBoxLayout(centralWidget);
    mainLayout->setContentsMargins(5, 5, 5, 5); // 设置较小的边距
    mainLayout->setSpacing(5);                  // 设置较小的间距

    // 创一个QSplitter来允许用户调整三个区域的大小
    QSplitter *splitter = new QSplitter(Qt::Horizontal, centralWidget);
    mainLayout->addWidget(splitter);

    // 设置splitter的伸缩策略
    splitter->setStretchFactor(0, 1); // 左侧区域
    splitter->setStretchFactor(1, 2); // 中间区域
    splitter->setStretchFactor(2, 1); // 右侧区域

    // 设置区域（左侧）
    m_settingsArea = new SettingsArea(this);
    splitter->addWidget(m_settingsArea);

    // 图片预览区域（中间）
    m_imagePreviewArea = new ImagePreviewArea(this);
    splitter->addWidget(m_imagePreviewArea);

    // 文件信息区域（右侧）
    m_fileInfoArea = new FileInfoArea(this);
    splitter->addWidget(m_fileInfoArea);

    // 修改最小宽度设置
    m_settingsArea->setMinimumWidth(400);
    m_imagePreviewArea->setMinimumWidth(400);
    m_fileInfoArea->setMinimumWidth(200);

    // 修改窗口的最小尺寸
    setMinimumSize(1000, 600);

    // 调整splitter的初始大小比例
    QList<int> sizes;
    int totalWidth = this->width();
    sizes << totalWidth * 0.40 << totalWidth * 0.40 << totalWidth * 0.20;
    splitter->setSizes(sizes);

    // 添加事件过滤器来处理窗口大小变化
    this->installEventFilter(this);
}

void MainWindow::connectSignals()
{
    connect(m_settingsArea, &SettingsArea::imageListUpdated, this, &MainWindow::updateImageList);
    connect(m_imagePreviewArea, &ImagePreviewArea::imageSelected, this,
            &MainWindow::displayFileInfo);
}

void MainWindow::updateImageList(const QVector<FileInfo> &imageList)
{
    m_currentImageList = imageList;
    m_imagePreviewArea->setImageList(m_currentImageList);
}

void MainWindow::displayFileInfo(const FileInfo &fileInfo)
{
    m_fileInfoArea->displayFileInfo(fileInfo);
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == this && event->type() == QEvent::Resize)
    {
        QSplitter *splitter = findChild<QSplitter *>();
        if (splitter)
        {
            QList<int> sizes = splitter->sizes();
            int totalWidth   = sizes[0] + sizes[1] + sizes[2];

            // 保持比例
            sizes[0] = totalWidth * 0.40;
            sizes[1] = totalWidth * 0.40;
            sizes[2] = totalWidth * 0.20;

            splitter->setSizes(sizes);
        }
    }
    return QMainWindow::eventFilter(watched, event);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    // 停止人脸识别进程
    FaceRecognition::getInstance().stopProcess();
    
    // 调用父类的closeEvent
    QMainWindow::closeEvent(event);
}
